const alckSocket = {
	//是否全局websocket 
	isglobalsocket: function() {
		alckSocket.connectSocket(false, false, true)
	},

	//连接socket方法
	connectSocket: function(a, b, c) {
		if(!uni.getStorageSync("TK")){
			return
		}
		let socketDataController = uni.getStorageSync("socketDataController")
		
		socketDataController.isFiristConnected = c //记录是否第一次连接

		if (a == false && b == false) {
			socketDataController.connected = a
			socketDataController.connecting = b
		}
		if (socketDataController.connected || socketDataController.connecting) {
			// uni.showModal({
			// 	content: '正在连接或者已经连接，请勿重复连接',
			// 	showCancel: false
			// });
			return false;
		}
		socketDataController.connecting = true;
		uni.setStorageSync("socketDataController", socketDataController)
		uni.connectSocket({
			url: 'ws://10.20.52.236:9530/Api/ws',
			// #ifdef MP
			header: {
				'content-type': 'application/json'
			},
			// #endif
			// #ifdef MP-WEIXIN
			method: 'GET',
			// #endif
			success(res) {
				console.log("这里是接口调用成功的回调，不是连接成功的回调，请注意");
				// 这里是接口调用成功的回调，不是连接成功的回调，请注意
			},
			fail(err) {
				// 这里是接口调用失败的回调，不是连接失败的回调，请注意
			}
		});
		uni.onSocketOpen(function(res) {
			//监听WebSocket连接打开事件。
			socketDataController.connecting = false;
			socketDataController.connected = true;
			console.log('WebSocket连接已打开！');
			// clearInterval(timer)
			socketDataController.timer = null
			socketDataController.timer = setInterval(() => {
				uni.sendSocketMessage({
					data: '{"type":"ping"}'
				});
				console.log('心跳中...');
			}, 40000)

			uni.setStorageSync("socketDataController", socketDataController)
		});

		uni.onSocketError(function(res) {
			//监听WebSocket错误。
			socketDataController.connecting = false;
			socketDataController.connected = false;
			uni.setStorageSync("isConnected", false)
			uni.hideLoading();
			console.log('连接失败，可能是websocket服务不可用，请稍后再试', res);
			uni.setStorageSync("socketDataController", socketDataController)
		});

		uni.onSocketMessage(function(res) {
			//监听WebSocket接受到服务器的消息事件。
			console.log("接收消息", res);
			return
			let mesData = JSON.parse(res.data);
			if (mesData.msg_type == "init") {
				// client_data = mesData;
				// alckSocket.bandNotice(mesData.client_id)
			} else {
				console.log("接收消息", mesData);
				uni.$emit('update', JSON.stringify(mesData))
				// #ifdef APP-PLUS
				uni.vibrateLong({
					success: function() {
						console.log('success');
					}
				});
				// #endif
			}
		});

		uni.onSocketClose(function(res) {
			//监听WebSocket关闭。
			socketDataController.connected = false;
			socketDataController.socketMsg = false;
			uni.setStorageSync("isConnected", false)
			clearInterval(socketDataController.timer)
			console.log("链接关闭", res);
			uni.closeSocket()
			uni.setStorageSync("socketDataController", socketDataController)
		});
	},


	//关闭socket
	closeSocket: function() {
		// let socketDataController = uni.getStorageSync("socketDataController")
		// clearInterval(socketDataController.timer)
		uni.closeSocket()
	},

	//tabbar 设置红点
	setTabbarRedDot: function() {
		uni.showTabBarRedDot({
			index: 3
		})
	},
	//tabbar 隐藏红点
	hideTabbarRedDot: function() {
		uni.hideTabBarRedDot({
			index: 3
		})
	},
}

export default alckSocket
